package com.example.order.service.impl;

import com.example.bean.order.Order;
import com.example.bean.product.Product;
import com.example.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    RestTemplate restTemplate;

    @Autowired //一定要导入Spring-cloud-starter-loadbalancer
    LoadBalancerClient loadBalancerClient;

    @Override
    public Order createOrder(Long userId, Long productId) {
        Product product = getProductFromRemoteWithLoadBalanceAnnotation(productId);
        Order order = new Order();
        order.setId(1L);
        //TODO 总金额
        order.setTotalAmount(product.getPrice().multiply(new BigDecimal(product.getNum())));
        order.setUserId(userId);
        order.setNickName("zhangsan");
        order.setAddress("南京");
        //TODO 远程查询商品列表
        order.setProductList(Arrays.asList(product));
        return order;
    }

    /**
     * 1.通过instances.get(0)调用服务
     * @param productId
     * @return
     */
    private Product getProductFromRemote(Long productId) {
        //获取到商品服务所在的所有ip和host
        List<ServiceInstance> instances = discoveryClient.getInstances("service-product");
        ServiceInstance serviceInstance = instances.get(0);
        //远程调用地址为http://localhost:9000/product/2
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort() + "/product/" + productId;

        log.info("远程调用地址：{}", url);
        //远程发起请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 2.通过负载均衡策略调用服务
     */
    private Product getProductFromRemoteWithLoadBalance(Long productId) {
        //获取到商品服务所在的所有ip和host
        ServiceInstance instance = loadBalancerClient.choose("service-product");
        //远程调用地址为http://localhost:9000/product/2
        String url = "http://" + instance.getHost() + ":" + instance.getPort() + "/product/" + productId;

        log.info("远程调用地址：{}", url);
        //远程发起请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }

    /**
     * 3.通过给restTemplate添加@LoadBalanced注解实现负载均衡
     * @param productId
     * @return
     */
    private Product getProductFromRemoteWithLoadBalanceAnnotation(Long productId) {
        String url = "http://service-product/product/" + productId;
        //远程发起请求
        Product product = restTemplate.getForObject(url, Product.class);
        return product;
    }


}
